package jp.co.dailyReport.project.sql;

import java.util.Vector;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import org.apache.log4j.Logger;
import jp.co.dailyReport.Executer;
import jp.co.dailyReport.Utilities;
import jp.co.dailyReport.project.Project;

/**
 * プロジェクト情報を取得するクラスです。
 */
public class ProjectLoadExecuter implements Executer {

	private static Logger log = Logger.getLogger(ProjectLoadExecuter.class);
	private int projectId;

	public ProjectLoadExecuter(int projectId) {
		this.projectId = projectId;
	}

	/**
	 * プロジェクトIDを基に、プロジェクト情報を取得します。
	 * 
	 * @param conn コネクションオブジェクト
	 * @return プロジェクトオブジェクトの配列
	 * @throws SQLException
	 */
	public Object[] execute(Connection conn) throws SQLException {

		ResultSet rset = null;
		PreparedStatement pstmt = null;
		String sql = "select * from project where projectId=?";
		log.debug("sql: " + sql);
		Vector answer = new Vector();
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, projectId);
			Project project;
			for(rset = pstmt.executeQuery(); rset.next(); answer.add(project)) {
				project = new Project();
				project.setProjectId(rset.getInt("projectId"));
				project.setCustomerId(rset.getInt("customerId"));
				project.setName(Utilities.convUniqueCharacter(rset.getString("name")));
				project.setPeriodStart(Utilities.getCalendar(rset.getDate("periodStart")));
				project.setPeriodEnd(Utilities.getCalendar(rset.getDate("periodEnd")));
				project.setPLStaffId(rset.getInt("pLStaffId"));
				project.setOutline(Utilities.convUniqueCharacter(rset.getString("outline")));
				project.setPjCustomerStaff(Utilities.convUniqueCharacter(rset.getString("pjCustomerStaff")));
				project.setStatusFlg(Utilities.decimalToBoolean(rset.getInt("statusFlg")));
				project.setDelFlg(Utilities.decimalToBoolean(rset.getInt("delFlg")));
				project.setUpDay(Utilities.getCalendar(rset.getDate("upDay")));
			}
		} finally {
			if(rset != null)
			 rset.close();
			if(pstmt != null)
			 pstmt.close();
		}
		return (Project[])answer.toArray(new Project[0]);
	}
}
